{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Variables, Functions, and Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Douglas Blank, Bryn Mawr College, CS206, Spring 2016

\n", "\n", "**Topics**:\n", "\n", "* Variables and Types\n", "* Primitive Java Types\n", "* What is a byte?\n", "* What is a nibble?\n", "* What is a bit?\n", "* History of Java\n", "* Functions and Classes\n", "* Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 Variables and Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create a variable, there is always an associated \"type\", and sometimes a \"value.\"\n", "\n", "```java\n", "TYPE NAME;\n", "TYPE NAME = VALUE;\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first form:\n", "\n", "* creates a variable with a specified type\n", "\n", "The second form does two things:\n", "\n", "* creates a variable with a specified type\n", "* initializes the variable with the given value" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable x of type int\n", "\n", "| Added variable s of type String\n", "\n", "| Added variable first_time of type boolean\n", "\n", "| Added variable b of type byte\n", "\n", "| Added variable total of type double\n", "\n" ] } ], "source": [ "int x; // These do not initialize\n", "String s;\n", "boolean first_time;\n", "byte b;\n", "double total;" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added method println(Object)\n", "\n", "\n", "| Added method print(Object)\n", "\n" ] } ], "source": [ "void println(Object thing) {\n", " System.out.println(thing);\n", "}\n", "\n", "void print(Object thing) {\n", " System.out.print(thing);\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "null\n", "false\n", "0\n", "0.0\n", "\n" ] } ], "source": [ "{\n", " println(x);\n", " println(s);\n", " println(first_time);\n", " println(b);\n", " println(total);\n", "}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable x of type int with initial value 5\n", "\n", "| Modified variable s of type String with initial value \"Hello\"\n", "\n", "| Modified variable first_time of type boolean with initial value true\n", "\n", "| Modified variable b of type byte with initial value 0\n", "\n", "| Modified variable total of type double with initial value 0.0\n", "\n" ] } ], "source": [ "int x = 5;\n", "String s = \"Hello\";\n", "boolean first_time = true;\n", "byte b = 0;\n", "double total = 0.0;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### Expressions and Statements\n", "\n", "Java is composed of \"expressions\", \"statements\" and \"blocks\". \n", "\n", "* An expression is something that evaluates to a value\n", "* A statement is a line that ends in a semicolon\n", "* A block is a group of statements that starts and stops curly-braces" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "| Expression value is: 3\n", "| assigned to temporary variable $20 of type int\n", "\n", "| Expression value is: true\n", "| assigned to temporary variable $21 of type boolean\n", "\n", "| Expression value is: 9\n", "| assigned to temporary variable $22 of type int\n", "\n" ] }, { "data": { "text/plain": [ "9" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// expressions\n", "\n", "1 + 2\n", "true\n", "(1 + 2) * 3" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "| Modified variable x of type int with initial value 7\n", "\n", "| Variable x has been assigned the value 0\n", "\n" ] } ], "source": [ "// statements\n", "\n", "int x = 1 + 2 * 3;\n", "x = 1 /2;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "// blocks\n", "\n", "{\n", " printf(\"Hello\");\n", " printf(\"World\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.5 Bits and Bytes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What's a bit? \n", "\n", "* 8 bits - \"binary digit\"\n", "* has the value 0 or 1\n", "\n", "All data, numbers, music, movies, etc. are at the bottom made up of 0s and 1s.\n", "\n", "We group bits into groups:\n", "\n", "* 8 bits are called a byte\n", "* 4 bits is called a nibble\n", "\n", "We can represent integers by allowing each position in a byte to represent the numbers of powers of two:\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
2 to the 7 2 to the 6 2 to the 5 2 to the 4 2 to the 3 2 to the 2 2 to the 1 2 to the 0 Integer
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1
1 1 1 1 1 1 1 1 255
" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "| Expression value is: 255.0\n", "| assigned to temporary variable $26 of type double\n", "\n" ] }, { "data": { "text/plain": [ "255.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import java.lang.Math;\n", "\n", "(Math.pow(2, 7) * 1 + \n", " Math.pow(2, 6) * 1 + \n", " Math.pow(2, 5) * 1 + \n", " Math.pow(2, 4) * 1 + \n", " Math.pow(2, 3) * 1 + \n", " Math.pow(2, 2) * 1 + \n", " Math.pow(2, 1) * 1 + \n", " Math.pow(2, 0) * 1)\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added method bin2int(int,int,int,int,int,int,int,int)\n", "\n" ] } ], "source": [ "double bin2int(int b8, int b7, int b6, int b5, \n", " int b4, int b3, int b2, int b1) {\n", " return (Math.pow(2, 7) * b8 + \n", " Math.pow(2, 6) * b7 + \n", " Math.pow(2, 5) * b6 + \n", " Math.pow(2, 4) * b5 + \n", " Math.pow(2, 3) * b4 + \n", " Math.pow(2, 2) * b3 + \n", " Math.pow(2, 1) * b2 + \n", " Math.pow(2, 0) * b1);\n", "}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: 87.0\n", "| assigned to temporary variable $30 of type double\n", "\n" ] }, { "data": { "text/plain": [ "87.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin2int(0, 1, 0, 1, 0, 1, 1, 1)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Replaced method bin2int(int,int,int,int,int,int,int,int)\n", "| Update overwrote method bin2int(int,int,int,int,int,int,int,int)\n", "\n" ] } ], "source": [ "int bin2int(int b8, int b7, int b6, int b5, \n", " int b4, int b3, int b2, int b1) {\n", " double d = (Math.pow(2, 7) * b8 + \n", " Math.pow(2, 6) * b7 + \n", " Math.pow(2, 5) * b6 + \n", " Math.pow(2, 4) * b5 + \n", " Math.pow(2, 3) * b4 + \n", " Math.pow(2, 2) * b3 + \n", " Math.pow(2, 1) * b2 + \n", " Math.pow(2, 0) * b1);\n", " return ((int)d);\n", "}" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: 243\n", "| assigned to temporary variable $32 of type int\n", "\n" ] }, { "data": { "text/plain": [ "243" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin2int(1, 1, 1, 1, 0, 0, 1, 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fill out the following table:\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
2 to the 7 2 to the 6 2 to the 5 2 to the 4 2 to the 3 2 to the 2 2 to the 1 2 to the 0 Integer
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
1 0 0 0 1 0 1 0
0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1
7
22
113
256
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Signed integers**:\n", "\n", "* saves the left-most bit to represent the \"sign\" of the number: 0 is positive, 1 is negative\n", "* Pisces is not a sign here. Only 0 and 1\n", "\n", "**Unsigned integers**:\n", "\n", "* All bits are used to represent the number (eg, another power of two)\n", "\n", "#### Challenge\n", "\n", "Make a new function `bin2signedint` that will turn a series of bits into a signed integer.\n", "\n", "* What numbers can you no longer represent?\n", "* What numbers can you now represent?\n", "\n", "### Nibbles\n", "\n", "Two \"nibbles\", each have a range between 0 and 15\n", "\n", " 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F \n", " 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15\n", "\n", " 0001 1010\n", "\n", "The first nibble (on the left) is worth 16 * value, the second nibble is worth one * value\n", "\n", " 1111 1111\n", "\n", " (15 * 16) + 15 = 255\n", "\n", "**Nibbles in Binary, Decimal, and Hexadecimal**\n", "\n", " 0000 0 \n", " 0001 1 \n", " 0010 2 \n", " 0011 3 \n", " 0100 4 \n", " 0101 5 \n", " 0110 6 \n", " 0111 7 \n", " 1000 8 \n", " 1001 9 \n", " 1010 10 A \n", " 1011 11 B \n", " 1100 12 C \n", " 1101 13 D \n", " 1110 14 E \n", " 1111 15 F " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.3 Java Primitive Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Type | Value | Size | Range\n", "-----| ----- | ---- | ------\n", "boolean | true or false | 1 byte | true or false\n", "char | character (unicode) | 2 bytes | Any Unicode character, 0 to 65,535 \n", "byte | integer | 1 byte | -128 to 127, or 0 to 255\n", "short | integer | 2 bytes | -32,768 to 32,767\n", "int | integer | 4 bytes | -2,147,483,648 to 2,147,483,647\n", "long | integer | 8 bytes | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807\n", "float | floating-point | 4 bytes | 1.4e-45 to 3.4e38 (+/-)\n", "double | floating-point | 8 bytes | 4.94e-324 to 1.79e+308 (+/-)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.4 Type-checking" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "| Modified variable x of type int with initial value 1\n", "\n", "| Error:\n", "| incompatible types: java.lang.String cannot be converted to int\n", "| x = \"hello\"; // ERROR!\n", "| ^-----^\n", "\n" ] } ], "source": [ "int x = 1;\n", "x = \"hello\"; // ERROR!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "int y = 1;\n", "y = 2;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.6 T-Shirt and a Joke\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.7 Java History" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Created by Sun Microsystems team led by James Gosling (1991) \n", "1. Originally designed for programming home appliances; originally called \"Oak\"\n", " * \"A solution looking for a problem\"\n", " * Difficult task because appliances are controlled by a wide variety of computer processors \n", " * Writing a compiler (translation program) for each type of appliance processor would have been very costly\n", " * Solution: two-step translation process \n", " * compile for a Virtual Machine (VM)\n", " * interpreter on VM written for each real machine\n", "1. Java now owned by Oracle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.8 Functions and Classes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class SomeType {\n", " SomeType() {\n", " // constructor code!\n", " }\n", "}\n", "\n", "SomeType thing = new SomeType();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class SomeOtherType {\n", " static void main() {\n", " System.out.println(\"Hi!\");\n", " }\n", "}\n", "\n", "SomeOtherType.main();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.9 Loops and Ifs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes called \"control blocks\" or \"flow of control\"\n", "\n", "* if-else blocks\n", "* for loops\n", "* while loops\n", "* do-while loops\n", "\n", "#### if-else blocks\n", "\n", "```java\n", "if (TEST) {\n", "\tSTATEMENT;\n", "\t...\n", "} else if (TEST) {\n", "\tSTATEMENT;\n", "\t...\n", "} else {\n", "\tSTATEMENT;\n", "\t...\n", "}\n", "```\n", "\n", "```java\n", "if (x == 0) {\n", "\tSTATEMENT;\n", "\t...\n", "} else if (x == 1) {\n", "\tSTATEMENT;\n", "\t...\n", "} else {\n", "\tSTATEMENT;\n", "\t...\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.10 Incrementors and Decrementors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Straightforward**:\n", "\n", "```java\n", "i = i + 1;\n", "i = i - 1;\n", "```\n", "\n", "**Incrementors and decrementors**:\n", "\n", "* optimized\n", "\n", "```java\n", "// post increment\n", "i++; \n", "i--;\n", "// pre increment\n", "++i; \n", "--i;\n", "```\n", "\n", "**Incrementors and decrementors are expressions**:\n", "\n", "```java\n", "x = i++ + 7;\n", "x = ++i + 7;\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.11 for loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```java\n", "for (INIT; TEST; UPDATE) {\n", " STATEMENT;\n", " ...\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for (int i = 0; i < 10; i = i+1) {\n", " System.out.println(i);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.12 while loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```java\n", "INIT;\n", "while (TEST) {\n", " STATEMENT;\n", "\t\t...\n", "\tUPDATE;\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "int i = 0;\n", "while (i < 10) {\n", " System.out.println(i);\n", " i++;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.13 do-while loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```java\n", "INIT;\n", "do {\n", " STATEMENT;\n", " ...\n", " UPDATE;\n", "} while (TEST);\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "boolean getStatus() {\n", " counter++;\n", " return (counter != 10);\n", "}\n", "\n", "boolean trapped; \n", "int counter = 0;\n", "do {\n", " System.out.println(\"Help!\");\n", "\n", " trapped = getStatus();\n", "} while (trapped);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java9" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }